/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.Message;
import com.luck.easyfoodbatch.listener.OnItemClickListener;
import com.luck.easyfoodbatch.text.MultiActionClickableSpan;
import com.luck.easyfoodbatch.text.MultiActionTextView;
import com.luck.easyfoodbatch.ui.HttpActivity;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyrecyclerview.sideslip.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by YOLANDA on 2016/7/22.
 */
public class MenuSystemAdapter extends SwipeMenuAdapter<MenuSystemAdapter.DefaultViewHolder> {

    private List<Message> messages;
    public Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public MenuSystemAdapter(List<Message> messages, Context mContext) {
        this.messages = messages;
        this.mContext = mContext;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public int getItemCount() {
        return messages == null ? 0 : messages.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.system_item, parent, false);
    }

    @Override
    public MenuSystemAdapter.DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(MenuSystemAdapter.DefaultViewHolder holder, int position) {

        holder.setData(messages.get(position), mContext);
        holder.setOnItemClickListener(mOnItemClickListener);
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView tv_title, tv_time;
        private MultiActionTextView tv_content;
        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_content = (MultiActionTextView) itemView.findViewById(R.id.tv_content);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setData(final Message bean, final Context mContext) {
            String link_url = bean.getLink_url();
            tv_title.setText(bean.getTitle());
            String content = "";
            if (!Tools.isNull(link_url)) {
                String more = "点击查看>>" +
                        "";
                content = bean.getContent() + more;
                int colorRipple = ContextCompat.getColor(tv_content.getContext(), R.color.blue_color);
                MultiActionClickableSpan action = new MultiActionClickableSpan(content.length() - more.length(), content.length(), colorRipple, false, false, new MultiActionClickableSpan.OnTextClickedListener() {
                    @Override
                    public void onTextClicked(View view, MultiActionClickableSpan span) {
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getTitle());
                        bundle.putString("url", bean.getLink_url());
                        intent.setClass(mContext, HttpActivity.class);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
                tv_content.setText(content, action);
            } else {
                content = bean.getContent();
                tv_content.setText(content);
            }

            if (!TextUtils.isEmpty(bean.getCreated_at() + "")) {
                tv_time.setText(Tools.SubTime3(bean.getCreated_at() + ""));
            } else {
                tv_time.setText("");
            }
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }

}
